package com.itita.ww2.game.item;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.itita.ww2.model.item.ds.ItemValue;
import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class ItemResponse {
	private Set<ItemValue> append; // 新增的
	private Set<ItemValue> modify; // 修改的
	private Set<String> remove; // 移除的
	private ItemValue lastItem; // 最后一个操作的物品,merge操作的最后一个
	//
	private Object tempObj; // 临时对象

	public ItemResponse() {
		modify = new HashSet<>();
		remove = new HashSet<>();
		append = new HashSet<>();
	}

	public Object getTempObj() {
		return tempObj;
	}

	public void setTempObj(Object tempObj) {
		this.tempObj = tempObj;
	}

	public void append(ItemValue item) {
		this.append.add(item);
		this.lastItem = item;
	}

	public void addModify(ItemValue item) {
		this.modify.add(item);
		this.lastItem = item;
	}

	public void addRemove(String uuid) {
		remove.add(uuid);
	}

	private void addOther(ItemResponse ir) {
		this.modify.addAll(ir.getModify());
		this.remove.addAll(ir.getRemove());
		this.append.addAll(ir.getAppend());
		this.lastItem = ir.getLastItem();
	}

	public ItemResponse merge(ItemResponse... responses) {
		for (ItemResponse item : responses) {
			this.addOther(item);
		}
		return this;
	}

	/**
	 * 合并多个Response对象<br>
	 * lastItem将保持参数responses的最后一个值
	 */
	public static ItemResponse merge(List<ItemResponse> responses) {
		ItemResponse ref = new ItemResponse();
		for (ItemResponse item : responses) {
			ref.addOther(item);
		}
		return ref;
	}

	public ISFSObject toSFSObject() {
		ISFSObject ref = SFSObject.newInstance();
		ISFSArray appendArr = SFSArray.newInstance();
		ISFSArray modifyArr = SFSArray.newInstance();
		// ISFSArray removeArr = SFSArray.newInstance();

		for (ItemValue item : append) {
			appendArr.addSFSObject(item.toSFSObject());
		}
		for (ItemValue item : modify) {
			modifyArr.addSFSObject(item.toSFSObject());
		}
		// removeArr.addUtfStringArray(this.remove);

		ref.putSFSArray("append", appendArr);
		ref.putSFSArray("modify", modifyArr);
		ref.putUtfStringArray("remove", this.remove);
		return ref;
	}

	public Set<ItemValue> getModify() {
		return modify;
	}

	public Set<String> getRemove() {
		return remove;
	}

	public Set<ItemValue> getAppend() {
		return append;
	}

	public ItemValue getLastItem() {
		return lastItem;
	}

	public void setLastItem(ItemValue lastItem) {
		this.lastItem = lastItem;
	}
}
